package com.william.jdbcplus.boot.test.dao.impl;

import com.william.jdbcplus.boot.starter.wrap.QueryLambdaBootstrap;
import com.william.jdbcplus.boot.test.dao.SysDepartRoleDao;
import com.william.jdbcplus.boot.test.entity.SysDepart;
import com.william.jdbcplus.boot.test.entity.SysDepartRole;
import com.william.jdbcplus.boot.test.entity.SysUserDepart;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@AllArgsConstructor
public class SysDepartRoleDaoImpl implements SysDepartRoleDao {

    @Override
    public List<SysDepartRole> queryDeptRoleByDeptAndUser(String orgCode, String userId) {
        //        SELECT * FROM sys_depart_role WHERE depart_id IN (
        //                SELECT id FROM sys_depart WHERE id IN (SELECT dep_id FROM sys_user_depart WHERE user_id=#{userId})
        //        AND org_code LIKE CONCAT(#{orgCode},'%')
        //        )

        QueryLambdaBootstrap bootstrap = new QueryLambdaBootstrap();
        bootstrap
                .selectAll(SysDepartRole.class)
                .from(SysDepartRole.class)
                .in(SysDepartRole::getDepartId, lb -> lb
                        .select(SysDepart::getId)
                        .from(SysDepart.class)
                        .in(SysDepart::getId, e -> e
                                .select(SysUserDepart::getDepId)
                                .from(SysUserDepart.class)
                                .eq(SysUserDepart::getUserId, userId)
                        )
                        .likeLeft(SysDepart::getOrgCode, orgCode)
                );

        return bootstrap.findList(SysDepartRole.class);
    }
}
